/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.takeorder.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.SnowFlake;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.address.entity.Address;
import com.jeeplus.modules.address.service.AddressService;
import com.jeeplus.modules.centertakeposition.entity.CenterTakePosition;
import com.jeeplus.modules.centertakeposition.service.CenterTakePositionService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.ordersstore.entity.OrdersStore;
import com.jeeplus.modules.takegoods.entity.TakeGoods;
import com.jeeplus.modules.takegoods.service.TakeGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.takeorder.entity.TakeOrder;
import com.jeeplus.modules.takeorder.mapper.TakeOrderMapper;

/**
 * 提货订单Service
 * @author w
 * @version 2021-03-26
 */
@Service
@Transactional(readOnly = true)
public class TakeOrderService extends CrudService<TakeOrderMapper, TakeOrder> {
	@Autowired
	private TakeGoodsService takeGoodsService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private CenterTakePositionService centerTakePositionService;
	@Autowired
	private MemberService memberService;

	public TakeOrder get(String id) {
		return super.get(id);
	}
	
	public List<TakeOrder> findList(TakeOrder takeOrder) {
		return super.findList(takeOrder);
	}
	
	public Page<TakeOrder> findPage(Page<TakeOrder> page, TakeOrder takeOrder) {
		dataRuleFilter(takeOrder);
		takeOrder.setPage(page);
		if (StringUtils.isNotBlank(takeOrder.getServiceId())){
			Member memberByLoginName = memberService.getMemberByLoginName(takeOrder.getServiceId());
			if (memberByLoginName != null){
				takeOrder.setServiceId(memberByLoginName.getId());
			}else {
				page.setList(new ArrayList<>());
				return page;
			}

		}
		List<TakeOrder> list = super.findList(takeOrder);
		if (list.size()>0){
			for (TakeOrder takeOrder1 : list) {
				Member member = memberService.get(takeOrder1.getServiceId());
				if (member != null){
					takeOrder1.setServiceId(member.getLoginName());
				}
			}
		}
		page.setList(list);
		return page;
//		return super.findPage(page, takeOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(TakeOrder takeOrder) {
		if (takeOrder.getServiceMem() != null){
			if (StringUtils.isNotBlank(takeOrder.getServiceMem().getId())){
				takeOrder.setServiceId(takeOrder.getServiceMem().getId());
			}
		}
		takeOrder.setPayTime(takeOrder.getPayTime());
		super.save(takeOrder);
	}
	
	@Transactional(readOnly = false)
	public void delete(TakeOrder takeOrder) {
		super.delete(takeOrder);
	}

	/**
	 * 提货
	 * @param goodsId
	 * @param member
	 * @param num
	 * @param serviceId
	 * @return
	 */
	@Transactional(readOnly = false)
	public AjaxJson takeYourGoods(String goodsId, Member member, int num, String serviceId) {
		AjaxJson aj = new AjaxJson();
		TakeGoods takeGoods = takeGoodsService.get(goodsId);
		Account account = accountService.get(member.getAccount().getId());
		if (account != null){
			if (takeGoods != null){
				//查询积分中心对应库存
				CenterTakePosition centerTakePosition = new CenterTakePosition();
				centerTakePosition.setGoods(takeGoods);
				Member mem = memberService.get(serviceId);
				if (mem == null){
					aj.setSuccess(false);
					aj.setMsg("积分中心信息有误");
					return aj;
				}
				centerTakePosition.setMember(mem);
				List<CenterTakePosition> positionList = centerTakePositionService.findList(centerTakePosition);
				if (positionList.size() > 0){
					if (positionList.get(0).getStock() < num){
						aj.setSuccess(false);
						aj.setMsg("积分中心商品库存不足");
						return aj;
					}
					Double coupon = takeGoods.getGoodsPrice() * num;
					if (account.getExchangeCoupon() >= coupon){
						Address address = new Address();
						address.setMember(member);
						List<Address> list = addressService.findList(address);
						if (list.size()>0){
							SnowFlake snowFlake = new SnowFlake(2, 3);
							//生成订单编号
							Long orderFormNumber = snowFlake.nextId();
							TakeOrder takeOrder = new TakeOrder();
							takeOrder.setPayTime(new Date());
							takeOrder.setGoodsName(takeGoods.getGoodsName());
							takeOrder.setGoodsNum(num);
							takeOrder.setGoodsPrice(takeGoods.getGoodsPrice());
							takeOrder.setMember(member);
							takeOrder.setDeliveryMethod(takeGoods.getDeliveryMethod());
							takeOrder.setOrderNo(orderFormNumber.toString());
							takeOrder.setTakeGoods(takeGoods);
							takeOrder.setServiceId(serviceId);
							// 订单状态  待发货
							takeOrder.setOrderStatus("0");
							takeOrder.setTotalPrice(takeGoods.getGoodsPrice()*num);
							//收货地址
							for (Address address1 : list) {
								if ("1".equals(address1.getIsdefault())){
									takeOrder.setAddress(address1.getAddress());
									takeOrder.setMobile(address1.getMobile());
									takeOrder.setName(address1.getName());
								}
							}
							save(takeOrder);
							//扣兑换券
							account.setExchangeCoupon(account.getExchangeCoupon()-coupon);
							accountService.save(account);
							//减库存
							positionList.get(0).setStock(positionList.get(0).getStock() - num);
							centerTakePositionService.save(positionList.get(0));
							aj.setSuccess(true);
							aj.setMsg("提货成功");
						}else {
							aj.setSuccess(false);
							aj.setMsg("请优先绑定收货地址");
						}
					}else {
						aj.setSuccess(false);
						aj.setMsg("兑换券不足");
					}
				}else {
					aj.setSuccess(false);
					aj.setMsg("积分中心商品库存不足");
				}
			}else {
				aj.setSuccess(false);
				aj.setMsg("商品信息有误");
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("账户信息有误");
		}

		return aj;
	}



	/**
	 * 完成提货
	 * @param member
	 * @param orderNo
	 * @return
	 */
	@Transactional(readOnly = false)
	public AjaxJson completeTake(Member member, String orderNo) {
		AjaxJson aj = new AjaxJson();
		TakeOrder takeOrder = new TakeOrder();
		takeOrder.setOrderNo(orderNo);
		takeOrder.setMember(member);
		List<TakeOrder> list = findList(takeOrder);
		if (list.size()>0){
			for (TakeOrder order : list) {
				order.setOrderStatus("2");
				save(takeOrder);
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("订单信息有误");
		}
		return aj;
	}

	/**
	 * 平台发货更改
	 * @param takeOrder
	 */
	@Transactional(readOnly = false)
	public void deliver(TakeOrder takeOrder) {
		if ("0".equals(takeOrder.getOrderStatus())){
			takeOrder.setOrderStatus("1");
			save(takeOrder);
		}
	}

	/**
	 * 平台取消发货
	 * @param takeOrder
	 */
	@Transactional(readOnly = false)
	public void cancelDeliver(TakeOrder takeOrder) {
		if ("0".equals(takeOrder.getOrderStatus())){
			//查询提货用户，返还提货券
			Member member = memberService.get(takeOrder.getMember().getId());
			if (member != null){
				Account account = accountService.get(member.getAccount().getId());
				if (account != null){
					//返还兑换券
					account.setExchangeCoupon(account.getExchangeCoupon()+takeOrder.getTotalPrice());
					accountService.save(account);
				}
			}
			//查询对应积分中心，返还对应持有库存
			Member mem = memberService.get(takeOrder.getServiceId());
			if (mem != null){
				//查询积分中心对应库存
				CenterTakePosition centerTakePosition = new CenterTakePosition();
				centerTakePosition.setGoods(takeOrder.getTakeGoods());
				centerTakePosition.setMember(mem);
				List<CenterTakePosition> list = centerTakePositionService.findList(centerTakePosition);
				if (list.size() > 0){
					list.get(0).setStock(list.get(0).getStock() + takeOrder.getGoodsNum());
					centerTakePositionService.save(list.get(0));
				}
			}
			//已取消
			takeOrder.setOrderStatus("2");
			save(takeOrder);
		}
	}

	public List<TakeOrder> findLimitList(TakeOrder takeOrder) {
		return mapper.findLimitList(takeOrder);
	}
}